*************************
*************************
***Instrumentos Valora***
*************************
*************************

clear
set more off


use "$base_out/Observacion_indices_compliance.dta"

***Traigo base de herramientas***

merge 1:1 cct using "$base_out/Herramientas_indices_compliance.dta", gen(_herramientas)

***Dejo solo la muestra con observacion en los indicadores de interes***

keep if sis_participo_11_39!=. | sis_participo_40!=. 

merge 1:1 cct using "$basein/Datos adicionales/Tratamientos_completos.dta"

tab _m

keep if _merge==3

rename cct school_id

merge m:1 school_id using "$basein/Datos adicionales/estratos.dta", gen(_estratos)

drop if estrato==13 | estrato==15

merge 1:1 school_id using "$basein/Datos adicionales/secundarias.dta", gen(_secundaria)

tab _me

keep if _secundaria==3

******************
***KEEP´ONLY PEC VS PEC PLUS******
******************

drop if state==10
drop if treatgroup2=="Control"
drop if treatgroup2==""

*****************************
***Elimino las secundarias***
*****************************

drop if secondary==1

drop if treatgroup2==""

gen PEC_Plus=0
replace PEC_Plus=1 if treatgroup2=="PEC Plus"

gen PEC=0
replace PEC=1 if treatgroup2=="PEC"

label var PEC_Plus " "
label var PEC" "


**********************************
*****Estandarizo los índices******
**********************************

foreach x in Indice_total Indice_uso IndiceH_total IndiceH_uso {

egen std_`x'=std(`x')

}
replace sis_participo_11_39=1 if sis_participo_40==1
label var sis_participo_40 "All training sessions (40 hours)"
label var sis_participo_11_39 "Some training sessions (10-40 hours)"
*************************************
********BALANCE TABLE ***************
*************************************
eststo clear
eststo:  my_ptest_pec_vs_pecplus sis_participo_40 sis_participo_11_39, by(treatgroup2) clus_id(school_id) strat_id(estrato)
 esttab  using "$tables/ComplianceSISAT.tex", label replace  nolines nogaps fragment ///
	 star(* 0.1 ** 0.05 *** 0.01)  ///
	 mlabels(none) nomtitles nonumbers noobs nodep collabels(none)  ///
	 cells("mu_2(fmt(%9.2fc)) mu_3(fmt(%9.2fc) ) dif2(fmt(%9.2fc) star pvalue(d_p2))" "se_2(par) se_3(par) des_2(par)") 
	 
	 
	 
	 
 foreach var in sis_participo_40{
			

		reghdfe `var'  TD1 TD2,  nocons vce(cluster school_id) absorb(i.estrato)
		
		sum `var' if TD1==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_Control.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		sum `var' if TD2==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_PecPlus.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		

		
		reghdfe `var'   TD1 TD2 if treatgroup!="Control" & state!=10,  vce(cluster school_id) absorb(i.estrato)
		sum `var' if (TD1==1 | TD2==1) & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_Dif2.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		
}


keep school_id id_escuela sis_participo_11_39 sis_participo_40


save "$base_out/Sisat_Matching.dta", replace